package com.android.lovingpet.common.postlist;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.bean.Post;
import com.android.lovingpet.bean.Team;
import com.android.lovingpet.common.teaminfo.TeamInfoActivity;
import com.android.lovingpet.common.userinfo.UserInfoActivity;
import com.android.lovingpet.community.posting.AlbumAdapter;
import com.android.lovingpet.community.posting.PostingActivity;
import com.android.lovingpet.util.Utility;
import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

public class PostListAdapter extends RecyclerView.Adapter<PostListAdapter.ViewHolder> {

    private Context context;
    private List<Post> posts;

    public PostListAdapter(List<Post> posts) {
        this.posts = posts;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_post, parent, false);
        ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(v -> {
            int position = holder.getAdapterPosition();
            toInfo(position, false);
        });
        view.findViewById(R.id.iv_comment).setOnClickListener(v -> {
            int position = holder.getAdapterPosition();
            toInfo(position, true);
        });
        view.findViewById(R.id.iv_head).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Post post = posts.get(position);
                if (post.getUserType() == 1) {
                    Account account = Utility.getAccount(post.getUserId());
                    if (account != null) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra("account", account);
                        context.startActivity(intent);
                    }
                } else {
                    Team team = Utility.getTeam(post.getUserId());
                    if (team != null) {
                        Intent intent = new Intent(context, TeamInfoActivity.class);
                        intent.putExtra("team", team);
                        context.startActivity(intent);
                    }
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Post post = posts.get(position);
        if (post.getUserType() == 1) {
            Account account = Utility.getAccount(post.getUserId());
            if (account != null) {
                Glide.with(context).load(account.getHead()).into(holder.ivHead);
                holder.tvName.setText(account.getName());
            }
        } else {
            Team team = Utility.getTeam(post.getUserId());
            if (team != null) {
                Glide.with(context).load(team.getImage()).into(holder.ivHead);
                holder.tvName.setText(team.getName());
            }
        }
        holder.tvTime.setText(post.getCreateTime());
        holder.tvContext.setText(post.getContext());
        if (holder.recyclerView.getAdapter() == null) {
            holder.recyclerView.setAdapter(new AlbumAdapter(post.getImages()));
        } else {
            ((AlbumAdapter) holder.recyclerView.getAdapter()).setImages(post.getImages());
        }
        holder.recyclerView.getAdapter().notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    private void toInfo(int position, boolean isComment) {
        Intent intent = new Intent(context, PostingActivity.class);
        intent.putExtra("post", posts.get(position));
        intent.putExtra("isComment", isComment);
        context.startActivity(intent);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView ivHead;
        public TextView tvName;
        public TextView tvTime;
        public TextView tvContext;
        public RecyclerView recyclerView;
        public ImageView ivLike;
        public ImageView ivComment;

        public ViewHolder(View itemView) {
            super(itemView);
            ivHead = itemView.findViewById(R.id.iv_head);
            tvName = itemView.findViewById(R.id.tv_name);
            tvTime = itemView.findViewById(R.id.tv_variety);
            tvContext = itemView.findViewById(R.id.tv_context);
            recyclerView = itemView.findViewById(R.id.rv_album);
            recyclerView.setLayoutManager(new GridLayoutManager(itemView.getContext(), 3));
            //recyclerView.setNestedScrollingEnabled(false);
            ivLike = itemView.findViewById(R.id.iv_like);
            ivComment = itemView.findViewById(R.id.iv_comment);
        }
    }
}
